import * as Blockly from 'blockly/core';

export const ks_a_Write = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var dropdown_val = generator.valueToCode(this, 'num', generator.ORDER_ATOMIC);
    //generator.setups_['setup_output_'+dropdown_pin] = 'pinMode('+dropdown_pin+', OUTPUT);';
    var code = 'analogWrite(' + dropdown_pin + ',' + dropdown_val + ');\n'
    return code;
}

export const ks_led1 = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var dropdown_stat = this.getFieldValue('STAT');
    generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    var code = 'digitalWrite(' + dropdown_pin + ',' + dropdown_stat + ');\n'
    return code;
}

export const ks_ledw = ks_led1;
export const ks_ledr = ks_led1;
export const ks_ledg = ks_led1;
export const ks_ledb = ks_led1;
export const ks_ledy = ks_led1;
export const ks_led2 = ks_led1;
export const ks_led3 = ks_led1;
export const ks_led4 = ks_led1;
export const ks_led5 = ks_led1;
export const ks_led01 = ks_led1;
export const ks_led02 = ks_led1;
export const ks_led03 = ks_led1;
export const ks_led04 = ks_led1;
export const ks_led05 = ks_led1;

export const ks_y_buzzer = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var dropdown_stat = this.getFieldValue('STAT');
    generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    var code = 'digitalWrite(' + dropdown_pin + ',' + dropdown_stat + ');\n'
    return code;
}

export const ks_w_buzzer1 = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var dropdown_stat = this.getFieldValue('STAT');
    generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    var code = 'digitalWrite(' + dropdown_pin + ',' + dropdown_stat + ');\n'
    return code;
}

export const ks_w_buzzer2 = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var fre = generator.valueToCode(this, 'FREQUENCY',
        generator.ORDER_ASSIGNMENT) || '0';
    var code = "tone(" + dropdown_pin + "," + fre + ");\n";
    generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    return code;
}

export const ks_w_buzzer3 = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var fre = generator.valueToCode(this, 'FREQUENCY',
        generator.ORDER_ASSIGNMENT) || '0';
    var dur = generator.valueToCode(this, 'DURATION',
        generator.ORDER_ASSIGNMENT) || '0';
    var code = "tone(" + dropdown_pin + "," + fre + "," + dur + ");\n";
    generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    return code;
}

////////////////////蜂鸣器//////////////////////
export const tone01 = function (_, generator) {
    var code = this.getFieldValue('STAT');
    return [code, generator.ORDER_ATOMIC];
}

export const buzzer = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var fre = generator.valueToCode(this, 'FREQUENCY',
        generator.ORDER_ASSIGNMENT) || '0';
    var dur = generator.valueToCode(this, 'DURATION',
        generator.ORDER_ASSIGNMENT) || '0';
    generator.setups_['setup_output'] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    var code = "";
    if (window.isNaN(dropdown_pin)) {
        code = code + 'pinMode(' + dropdown_pin + ', OUTPUT);\n';
    } else {
        generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    }
    code += "tone(" + dropdown_pin + "," + fre + "," + dur + ");\n";
    return code;
}

export const controls_tone2 = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var fre = generator.valueToCode(this, 'FREQUENCY',
        generator.ORDER_ASSIGNMENT) || '0';
    var dur = generator.valueToCode(this, 'DURATION',
        generator.ORDER_ASSIGNMENT) || '0';
    var code = "";
    if (window.isNaN(dropdown_pin)) {
        code = code + 'pinMode(' + dropdown_pin + ', OUTPUT);\n';
    } else {
        generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    }
    code += "tone(" + dropdown_pin + "," + fre + "," + dur + ");\n";
    return code;
}

//////////////////////////music///////////////////////////
export const music = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    generator.setups_['setup_output_music'] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    generator.definitions_['include_birthday'] = 'void birthday()\n{\n  tone(' + dropdown_pin + ',294);\n  delay(250);\n  tone(' + dropdown_pin + ',440);\n  delay(250);\n  tone(' + dropdown_pin + ',392);\n  delay(250);\n  tone(' + dropdown_pin + ',532);\n  delay(250);\n  tone(' + dropdown_pin + ',494);\n  delay(500);\n  tone(' + dropdown_pin + ',392);\n  delay(250);\n  tone(' + dropdown_pin + ',440);\n  delay(250);\n  tone(' + dropdown_pin + ',392);\n  delay(250);\n  tone(' + dropdown_pin + ',587);\n  delay(250);\n  tone(' + dropdown_pin + ',532);\n  delay(500);\n  tone(' + dropdown_pin + ',392);\n  delay(250);\n  tone(' + dropdown_pin + ',784);\n  delay(250);\n  tone(' + dropdown_pin + ',659);\n  delay(250);\n  tone(' + dropdown_pin + ',532);\n  delay(250);\n  tone(' + dropdown_pin + ',494);\n  delay(250);\n  tone(' + dropdown_pin + ',440);\n  delay(250);\n  tone(' + dropdown_pin + ',698);\n  delay(375);\n  tone(' + dropdown_pin + ',659);\n  delay(250);\n  tone(' + dropdown_pin + ',532);\n  delay(250);\n  tone(' + dropdown_pin + ',587);\n  delay(250);\n  tone(' + dropdown_pin + ',532);\n  delay(500);\n}\n';
    generator.definitions_['include_tone'] = '//tone\n#define D0 -1\n#define D1 262\n#define D2 293\n#define D3 329\n#define D4 349\n#define D5 392\n#define D6 440\n#define D7 494\n#define M1 523\n#define M2 586\n#define M3 658\n#define M4 697\n#define M5 783\n#define M6 879\n#define M7 987\n#define H1 1045\n#define H2 1171\n#define H3 1316\n#define H4 1393\n#define H5 1563\n#define H6 1755\n#define H7 1971\n\n#define WHOLE 1\n#define HALF 0.5\n#define QUARTER 0.25\n#define EIGHTH 0.25\n#define SIXTEENTH 0.625\n ';

    generator.definitions_['include_tune'] = '\nint tune[]= \n{\n  M3,M3,M4,M5,\n  M5,M4,M3,M2,\n  M1,M1,M2,M3,\n  M3,M2,M2,\n  M3,M3,M4,M5,\n  M5,M4,M3,M2,\n  M1,M1,M2,M3,\n  M2,M1,M1,\n  M2,M2,M3,M1,\n  M2,M3,M4,M3,M1,\n  M2,M3,M4,M3,M2,\n  M1,M2,D5,D0,\n  M3,M3,M4,M5,\n  M5,M4,M3,M4,M2,\n  M1,M1,M2,M3,\n  M2,M1,M1\n};';
    generator.definitions_['include_durt'] = '\nfloat durt[]= \n {\n  1,1,1,1,\n  1,1,1,1,\n  1,1,1,1,\n  1+0.5,0.5,1+1,\n  1,1,1,1,\n  1,1,1,1,\n  1,1,1,1,\n  1+0.5,0.5,1+1,\n  1,1,1,1,\n  1,0.5,0.5,1,1,\n  1,0.5,0.5,1,1,\n  1,1,1,1,\n  1,1,1,1,\n  1,1,1,0.5,0.5,\n  1,1,1,1,\n  1+0.5,0.5,1+1,\n };';
    generator.definitions_['include_io1'] = '\n int length;\n int tonepin=' + dropdown_pin + '; \n';
    generator.definitions_['include_Ode_to_Joy'] = 'void Ode_to_Joy()\n{\n  for(int x=0;x<length;x++)\n  {\n    tone(tonepin,tune[x]);\n    delay(300*durt[x]);   \n  }\n}\n';

    generator.setups_['setup_output_Ode'] = 'length=sizeof(tune)/sizeof(tune[0]);\n';
    var dropdown_type = this.getFieldValue('play');

    var code = '';
    if (dropdown_type == "Birthday") code += 'birthday();\n';
    if (dropdown_type == "Ode to Joy") code += 'Ode_to_Joy();\n';
    //if (dropdown_type == "City of Sky") code += 'digitalRead(8)';
    return code;
    //return [code, generator.ORDER_ATOMIC];
}

export const notone = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    generator.setups_['setup_output'] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    var code = '';
    code += "noTone(" + dropdown_pin + ");\n";
    return code;
}

//////////////////music/////////////////////////

export const ks_relay = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var dropdown_stat = this.getFieldValue('STAT');
    generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    var code = 'digitalWrite(' + dropdown_pin + ',' + dropdown_stat + ');\n'
    return code;
}

export const ks_motor = function (_, generator) {
    var dropdown_pin1 = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var dropdown_stat1 = this.getFieldValue('STAT1');
    generator.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';

    var dropdown_pin2 = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);
    var dropdown_stat2 = this.getFieldValue('STAT2');
    generator.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', OUTPUT);';

    var code = 'digitalWrite(' + dropdown_pin1 + ',' + dropdown_stat1 + ');\ndigitalWrite(' + dropdown_pin2 + ',' + dropdown_stat2 + ');\n'
    return code;
}

//舵机
export const ks_servo = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var value_degree = generator.valueToCode(this, 'angle', generator.ORDER_ATOMIC);
    //value_degree = value_degree.replace('(','').replace(')','')
    var delay_time = generator.valueToCode(this, 'time', generator.ORDER_ATOMIC) || '0'
    //delay_time = delay_time.replace('(','').replace(')','');

    generator.definitions_['include_Servo'] = '#include <Servo.h>';
    generator.definitions_['var_servo' + dropdown_pin] = 'Servo servo_' + dropdown_pin + ';';
    generator.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.attach(' + dropdown_pin + ');';

    var code = 'servo_' + dropdown_pin + '.write(' + value_degree + ');\n' + 'delay(' + delay_time + ');\n';
    return code;
}

export const ks_servo_r = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);

    generator.definitions_['include_Servo'] = '#include <Servo.h>';
    generator.definitions_['var_servo' + dropdown_pin] = 'Servo servo_' + dropdown_pin + ';';
    generator.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.attach(' + dropdown_pin + ');';

    var code = 'servo_' + dropdown_pin + '.read()';
    return [code, generator.ORDER_ATOMIC];
}


//////////////////////////数字传感器////////////////////////////////

export const ks_ir_g = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    generator.setups_['setup_input_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT);';
    var code = 'digitalRead(' + dropdown_pin + ')';
    return [code, generator.ORDER_ATOMIC];
}
/////////////////火焰传感器////////////////
export const ks_flame = ks_ir_g


/////////////////霍尔传感器////////////////
export const ks_hall = ks_ir_g


/////////////////碰撞传感器////////////////
export const ks_crash = ks_ir_g


/////////////////按键传感器////////////////
export const ks_button = ks_ir_g


/////////////////电容触摸传感器////////////////
export const ks_tuoch = ks_ir_g

/////////////////敲击传感器////////////////
export const ks_knock = ks_ir_g


/////////////////倾斜传感器////////////////
export const ks_tilt = ks_ir_g


/////////////////振动传感器////////////////
export const ks_shake = ks_ir_g


/////////////////干簧管传感器////////////////
export const ks_reed_s = ks_ir_g


/////////////////循迹传感器////////////////
export const ks_track = ks_ir_g


/////////////////避障传感器////////////////
export const ks_avoid = ks_ir_g

/////////////////光折断传感器////////////////
export const ks_light_b = ks_ir_g


////////////////////////////////////////////////////////
//////////////////////模拟传感器/////////////////////////
////////////////////////////////////////////////////////

export const ks_sound = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    generator.setups_['setup_input_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT);';
    var code = 'analogRead(' + dropdown_pin + ')';
    return [code, generator.ORDER_ATOMIC];
}

/////////////////////遥感///////////////////////////
export const ks_joystick = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    generator.setups_['setup_input_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT);';
    //var code = 'analogRead('+dropdown_pin+')';

    var dropdown_type = this.getFieldValue('joy');
    var code = '';
    if (dropdown_type == "Lx") code += 'analogRead(' + dropdown_pin + ')';
    if (dropdown_type == "Ly") code += 'analogRead(' + dropdown_pin + ')';
    if (dropdown_type == "Lz") code += 'digitalRead(2)';
    //if (dropdown_type == "Rx") code += 'analogRead('+dropdown_pin+')';
    //if (dropdown_type == "Ry") code += 'analogRead('+dropdown_pin+')';
    // if (dropdown_type == "Rz") code += 'digitalRead('+dropdown_pin+')';
    return [code, generator.ORDER_ATOMIC];
}


//LM35 Temperature
export const ks_lm35 = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    generator.setups_['setup_input_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT);';
    var code = 'analogRead(' + dropdown_pin + ')*0.488';
    return [code, generator.ORDER_ATOMIC];
}

export const ks_analog_t = ks_sound;

export const ks_light = ks_sound;

export const ks_water = ks_sound;

export const ks_soil = ks_sound;

export const ks_potentiometer = ks_sound;


export const ks_slide_potentiometer = ks_sound;

export const ks_temt6000 = ks_sound;

export const ks_steam = ks_sound;

export const ks_film_p = ks_sound;

//generator.ks_joystick = generator.ks_sound;

export const ks_smoke = ks_sound;

export const ks_alcohol = ks_sound;

export const ks_mq135 = ks_sound;

///////////////////////////////////////////////////
///////////////////传感器/////////////////////////
////////////////////////////////////////////////////

///////////////////////超声波//////////////////////
export const ks_sr01 = function (_, generator) {
    var dropdown_pin1 = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var dropdown_pin2 = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);
    generator.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';
    generator.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', INPUT);';
    var funcName = 'checkdistance_' + dropdown_pin1 + '_' + dropdown_pin2;
    var code = 'float' + ' ' + funcName + '() {\n'
        + '  digitalWrite(' + dropdown_pin1 + ', LOW);\n' + '  delayMicroseconds(2);\n'
        + '  digitalWrite(' + dropdown_pin1 + ', HIGH);\n' + '  delayMicroseconds(10);\n'
        + '  digitalWrite(' + dropdown_pin1 + ', LOW);\n'
        + '  float distance = pulseIn(' + dropdown_pin2 + ', HIGH) / 58.00;\n'
        + '  delay(10);\n' + '  return distance;\n'
        + '}\n';
    generator.definitions_[funcName] = code;
    return [funcName + '()', generator.ORDER_ATOMIC];
}

///////////////////18B20////////////////////////////
export const ks_18b20 = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var unit = this.getFieldValue('UNIT');
    generator.definitions_['include_OneWire'] = '#include <OneWire.h>';
    generator.definitions_['include_DallasTemperature'] = '#include <DallasTemperature.h>';
    generator.definitions_['var_OneWire_oneWire_' + dropdown_pin] = 'OneWire oneWire_' + dropdown_pin + '(' + dropdown_pin + ');';
    generator.definitions_['var_DallasTemperature_sensors_' + dropdown_pin] = 'DallasTemperature sensors_' + dropdown_pin + '(&oneWire_' + dropdown_pin + ');';
    generator.definitions_['var_DeviceAddress_insideThermometer'] = 'DeviceAddress insideThermometer;';
    generator.setups_['setup_sensors_' + dropdown_pin + '_getAddress'] = 'sensors_' + dropdown_pin + '.getAddress(insideThermometer, 0);';
    generator.setups_['setup_sensors_' + dropdown_pin + '_setResolution'] = 'sensors_' + dropdown_pin + '.setResolution(insideThermometer, 9);';
    var funcName = 'ds18b20_' + dropdown_pin + '_getTemp';
    var code = 'float' + ' ' + funcName + '(int w) {\n'
        + '  sensors_' + dropdown_pin + '.requestTemperatures();\n'
        + '  if(w==0) {\nreturn sensors_' + dropdown_pin + '.getTempC(insideThermometer);\n}\n'
        + '  else {\nreturn sensors_' + dropdown_pin + '.getTempF(insideThermometer);\n}\n'
        + '}\n';
    generator.definitions_[funcName] = code;
    return ['ds18b20_' + dropdown_pin + '_getTemp(' + unit + ')', generator.ORDER_ATOMIC];
}

//////////////////////////DHT11///////////////////////////
export const ks_dht11 = function (_, generator) {
    var sensor_type = this.getFieldValue('TYPE');
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var what = this.getFieldValue('WHAT');
    generator.definitions_['include_dht'] = '#include <dht.h>';
    generator.definitions_['var_dht_' + dropdown_pin] = 'dht myDHT_' + dropdown_pin + ';';
    var funcName = 'dht_' + dropdown_pin + '_get' + what;
    var code = 'int' + ' ' + funcName + '() {\n'
        + '  int chk = myDHT_' + dropdown_pin + '.read' + sensor_type + '(' + dropdown_pin + ');\n'
        + '  int value = myDHT_' + dropdown_pin + '.' + what + ';\n'
        + '  return value;\n'
        + '}\n';
    generator.definitions_[funcName] = code;
    return [funcName + '()', generator.ORDER_ATOMIC];
}

//传感器-重力感应块-获取数据
export const ks_adxl345 = function (_, generator) {
    generator.definitions_['include_ADXL345'] = '#include <ADXL345.h>';
    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['include_I2Cdev'] = '#include <I2Cdev.h>';
    generator.definitions_['var_ADXL345'] = 'ADXL345 accel;';
    generator.setups_['setup_ADXL345-A'] = 'Wire.begin();';
    generator.setups_['setup_ADXL345-B'] = 'accel.initialize();';
    var dropdown_type = this.getFieldValue('ADXL345_PIN');
    var code = '';
    if (dropdown_type == "xa") code += 'accel.X_angle()';
    if (dropdown_type == "ya") code += 'accel.Y_angle()';
    if (dropdown_type == "x") code += 'accel.getAccelerationX()';
    if (dropdown_type == "y") code += 'accel.getAccelerationY()';
    if (dropdown_type == "z") code += 'accel.getAccelerationZ()';
    return [code, generator.ORDER_ATOMIC];
}

///////////////////////BMP180温度大气压/////////////////////////////
export const ks_bmp180 = function (_, generator) {
    generator.definitions_['define_bmp180'] = '#define BMP180ADD 0xEE>>1';
    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['char_OSS'] = 'unsigned char OSS;';
    generator.definitions_['int'] = 'int ac1,ac2,ac3,b1,b2,mb,mc,md;';
    generator.definitions_['unsight_int'] = 'unsigned int ac4,ac5,ac6;';
    generator.definitions_['float'] = 'float temperature;';
    generator.definitions_['double'] = 'double pressure,pressure2,altitude;';
    generator.definitions_['long'] = 'long b5;';

    //generator.definitions_['calculate'] = 'void calculate()\n{\n  temperature = bmp180GetTemperature(bmp180ReadUT());\ntemperature = temperature*0.1;\npressure = bmp180GetPressure(bmp180ReadUP());\npressure2 = pressure/101325;\npressure2 = pow(pressure2,0.19029496);\naltitude = 44330*(1-pressure2);\n}';

    //generator.definitions_['show'] = 'void show()\n{\n  Serial.print("Temperature: ");\nSerial.print(temperature, 1);\nSerial.println(" C");\nSerial.print("Pressure: ");\nSerial.print(pressure, 0);\nSerial.println(" Pa");\nSerial.print("altitude:");\nSerial.print(altitude);\nSerial.println("m");\n}';

    generator.definitions_['BMP180start'] = 'void BMP180start()\n{\nac1 = bmp180ReadDate(0xAA);\nac2 = bmp180ReadDate(0xAC);\nac3 = bmp180ReadDate(0xAE);\nac4 = bmp180ReadDate(0xB0);\nac5 = bmp180ReadDate(0xB2);\nac6 = bmp180ReadDate(0xB4);\nb1  = bmp180ReadDate(0xB6);\nb2  = bmp180ReadDate(0xB8);\nmb  = bmp180ReadDate(0xBA);\nmc  = bmp180ReadDate(0xBC);\nmd  = bmp180ReadDate(0xBE);\n}';

    generator.definitions_['bmp180GetTemperature'] = 'short bmp180GetTemperature(unsigned int ut)\n{\nlong x1, x2;\nx1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;\nx2 = ((long)mc << 11)/(x1 + md);\nb5 = x1 + x2;\nreturn ((b5 + 8)>>4);\n}';

    generator.definitions_['bmp180GetPressure'] = 'long bmp180GetPressure(unsigned long up)\n{\nlong x1, x2, x3, b3, b6, p;\nunsigned long b4, b7;\nb6 = b5 - 4000;\nx1 = (b2 * (b6 * b6)>>12)>>11;\nx2 = (ac2 * b6)>>11;\nx3 = x1 + x2;\nb3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;\nx1 = (ac3 * b6)>>13;\nx2 = (b1 * ((b6 * b6)>>12))>>16;\nx3 = ((x1 + x2) + 2)>>2;\nb4 = (ac4 * (unsigned long)(x3 + 32768))>>15;\nb7 = ((unsigned long)(up - b3) * (50000>>OSS));\nif (b7 < 0x80000000)\n  p = (b7<<1)/b4;\nelse\n  p = (b7/b4)<<1;\nx1 = (p>>8) * (p>>8);\nx1 = (x1 * 3038)>>16;\nx2 = (-7357 * p)>>16;\np += (x1 + x2 + 3791)>>4;\nreturn p;\n}';

    generator.definitions_['bmp180Read'] = 'int bmp180Read(unsigned char address)\n{\nunsigned char data;\nWire.beginTransmission(BMP180ADD);\nWire.write(address);\nWire.endTransmission();\nWire.requestFrom(BMP180ADD, 1);\nwhile(!Wire.available());\nreturn Wire.read();\n}';

    generator.definitions_['bmp180ReadDate'] = 'int bmp180ReadDate(unsigned char address)\n{\nunsigned char msb, lsb;\nWire.beginTransmission(BMP180ADD);\nWire.write(address);\nWire.endTransmission();\nWire.requestFrom(BMP180ADD, 2);\nwhile(Wire.available()<2);\nmsb = Wire.read();\nlsb = Wire.read();\nreturn (int) msb<<8 | lsb;\n}';

    generator.definitions_['bmp180ReadUT'] = 'unsigned int bmp180ReadUT()\n{\nunsigned int ut;\nWire.beginTransmission(BMP180ADD);\nWire.write(0xF4);\nWire.write(0x2E);\nWire.endTransmission();\ndelay(5);\nut = bmp180ReadDate(0xF6);\nreturn ut;\n}';

    generator.definitions_['bmp180ReadUP'] = 'unsigned long bmp180ReadUP()\n{\nunsigned char msb, lsb, xlsb;\nunsigned long up = 0;\nWire.beginTransmission(BMP180ADD);\nWire.write(0xF4);\nWire.write(0x34 + (OSS<<6));\nWire.endTransmission();\ndelay(2 + (3<<OSS));\nWire.beginTransmission(BMP180ADD);\nWire.write(0xF6);\nWire.endTransmission();\nWire.requestFrom(BMP180ADD, 3);\nwhile(Wire.available() < 3);\nmsb = Wire.read();\nlsb = Wire.read();\nxlsb = Wire.read();\nup = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);\nreturn up;\n}';

    generator.definitions_['temperature1'] = 'float temperature1()\n{\ntemperature = bmp180GetTemperature(bmp180ReadUT());\ntemperature = temperature*0.1;\nreturn temperature;\n}';

    generator.definitions_['pressure1'] = 'double pressure1()\n{\nbmp180GetTemperature(bmp180ReadUT());\npressure = bmp180GetPressure(bmp180ReadUP());\nreturn pressure;\n}\n'

    generator.definitions_['altitude1'] = 'double altitude1()\n{\nbmp180GetTemperature(bmp180ReadUT());\npressure = bmp180GetPressure(bmp180ReadUP());\npressure2 = pressure/101325;\npressure2 = pow(pressure2,0.19029496);\naltitude = 44330*(1-pressure2);\nreturn altitude;\n}\n'

    generator.setups_['wire.begin'] = 'Wire.begin();';
    generator.setups_['OSS'] = 'OSS = 2;';
    generator.setups_['BMP180start'] = 'BMP180start();';


    var dropdown_type = this.getFieldValue('BMP180_PIN');
    var code = '';
    if (dropdown_type == "T") code += 'temperature1()';
    if (dropdown_type == "P") code += 'pressure1()';
    if (dropdown_type == "A") code += 'altitude1()';
    //return code;
    return [code, generator.ORDER_ATOMIC];
}

/////////////////////////////DS3231时钟/////////////////////////////
export const ks_ds3231 = function (_, generator) {
    var RTCName = this.getFieldValue('RTCName');
    var year = generator.valueToCode(this, "year", generator.ORDER_ATOMIC);
    var month = generator.valueToCode(this, "month", generator.ORDER_ATOMIC);
    var day = generator.valueToCode(this, "day", generator.ORDER_ATOMIC);
    var dow = generator.valueToCode(this, "dow", generator.ORDER_ATOMIC);
    var hour = generator.valueToCode(this, "hour", generator.ORDER_ATOMIC);
    var minute = generator.valueToCode(this, "minute", generator.ORDER_ATOMIC);
    var second = generator.valueToCode(this, "second", generator.ORDER_ATOMIC);


    generator.definitions_['include_DS3231_h'] = '#include <DS3231.h>';
    generator.definitions_['DS3231'] = 'DS3231  ' + RTCName + '(A4, A5);';

    generator.setups_['begin1'] = '' + RTCName + '.begin();';
    generator.setups_['setDOW'] = '' + RTCName + '.setDOW(' + dow + ');';
    generator.setups_['setTime'] = '' + RTCName + '.setTime(' + hour + ', ' + minute + ', ' + second + ');';
    generator.setups_['setDate'] = '' + RTCName + '.setDate(' + day + ', ' + month + ', ' + year + ');';

    var code = 'Serial.print(' + RTCName + '.getDOWStr());\nSerial.print(" ");\nSerial.print(' + RTCName + '.getDateStr());\nSerial.print(" -- ");\nSerial.println(' + RTCName + '.getTimeStr());\ndelay (1000);';

    return code;
}

/////////////////////////////DS3231获取时间////////////////////////
export const ks_ds3231getTime = function (_, generator) {
    var RTCName = this.getFieldValue('RTCName');
    generator.definitions_['include_DS3231_h'] = '#include <DS3231.h>';
    generator.definitions_['DS3231'] = 'DS3231  ' + RTCName + '(A4, A5);';
    generator.definitions_['Time'] = 'Time  t;';


    generator.definitions_['second1'] = 'int second1()\n{\nt = ' + RTCName + '.getTime();\nt.sec;\ndelay(1000);\nreturn t.sec;\n}';

    generator.definitions_['minute1'] = 'int minute1()\n{\nt = ' + RTCName + '.getTime();\nt.min;\nreturn t.min;\n}';

    generator.definitions_['hour1'] = 'int hour1()\n{\nt = ' + RTCName + '.getTime();\nt.hour;\nreturn t.hour;\n}';

    generator.definitions_['day1'] = 'int day1()\n{\nt = ' + RTCName + '.getTime();\nt.date;\nreturn t.date;\n}';

    generator.definitions_['week1'] = 'int week1()\n{\nt = ' + RTCName + '.getTime();\nt.dow;\nreturn t.dow;\n}';

    generator.definitions_['month1'] = 'int month1()\n{\nt = ' + RTCName + '.getTime();\n' + RTCName + '.getMonthStr();\nreturn ' + RTCName + '.getMonthStr();\n}';

    generator.definitions_['year1'] = 'int year1()\n{\nt = ' + RTCName + '.getTime();\nt.year;\nreturn t.year;\n}';

    generator.setups_['begin1'] = '' + RTCName + '.begin();';


    var dropdown_type = this.getFieldValue('TIME_TYPE');
    var code = '';
    //code += ''+timeType+' = '+RTCName+'.getTime();\n';
    if (dropdown_type == "getYear") code += 'year1()';
    if (dropdown_type == "getMonth") code += 'month1()';
    if (dropdown_type == "getDay") code += 'day1()';
    if (dropdown_type == "getHour") code += 'hour1()';
    if (dropdown_type == "getMinute") code += 'minute1()';
    if (dropdown_type == "getSecond") code += 'second1()';
    if (dropdown_type == "getWeek") code += 'week1()';

    //return code;
    return [code, generator.ORDER_ATOMIC];
}


////////////////////////////////////////////////////
//////////////////////////显示屏///////////////////
//////////////////////////////////////////////////////////

///////////////////////////RGB灯/////////////////
export const ks_rgb = function (_, generator) {
    var dropdown_rgbpin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var value_led = generator.valueToCode(this, 'num01', generator.ORDER_ATOMIC);
    var value_rvalue = generator.valueToCode(this, 'red', generator.ORDER_ATOMIC);
    var value_gvalue = generator.valueToCode(this, 'green', generator.ORDER_ATOMIC);
    var value_bvalue = generator.valueToCode(this, 'blue', generator.ORDER_ATOMIC);
    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
    if (!generator.definitions_['var_rgb_display' + dropdown_rgbpin]) {
        generator.definitions_['1var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '' + '(4);';
        generator.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
        generator.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
    }

    var code = 'rgb_display_' + dropdown_rgbpin + '.setPixelColor(' + value_led + '-1, ' + value_rvalue + ',' + value_gvalue + ',' + value_bvalue + ');\n';
    code += 'rgb_display_' + dropdown_rgbpin + '.show();\n';
    return code;
}

////////////////////////////TM1637数码管初始化//////////////////////////
export const ks_tm1637_init = function (_, generator) {
    var CLK = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var DIO = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);

    generator.definitions_['include_TM1637.h'] = '#include <TM1637.h>';
    generator.definitions_['TM1637'] = 'TM1637 TM(' + CLK + ', ' + DIO + ');';
    return '';
}

///////////////////////////TM1637数码管自定义显示/////////////////
export const ks_tm1637_dy = function (_, generator) {
    var num = generator.valueToCode(this, 'num', generator.ORDER_ATOMIC);
    //var tc = generator.valueToCode(this, 'tc', generator.ORDER_ATOMIC);
    var weishu = generator.valueToCode(this, 'weishu', generator.ORDER_ATOMIC);
    var wei = generator.valueToCode(this, 'wei', generator.ORDER_ATOMIC);
    var code = 'TM.cls();\nTM.Num(' + num + ',0,' + weishu + ',' + wei + ');' + '\n';
    return code;
}

///////////////////////////TM1637数码管自定义显示是否填充/////////////////
export const ks_tm1637_tc = function (_, generator) {
    var num = generator.valueToCode(this, 'num', generator.ORDER_ATOMIC);
    //var tc = generator.valueToCode(this, 'tc', generator.ORDER_ATOMIC);
    var tc = generator.valueToCode(this, '0and1', generator.ORDER_ATOMIC);
    var code = 'TM.cls();\nTM.Num(' + num + ',' + tc + ');' + '\n';
    return code;
}

///////////////////////////TM1637数码管亮度/////////////////
export const ks_tm1637_ld = function (_, generator) {
    var num = generator.valueToCode(this, 'num', generator.ORDER_ATOMIC);
    var code = 'TM.BL(' + num + ');' + '\n';
    return code;
}


///////////////////////////TM1637数码管是否隐藏/////////////////
export const ks_tm1637_yc = function (_, generator) {
    var bool = generator.valueToCode(this, 'BOOL', generator.ORDER_ATOMIC);
    var code = 'TM.show(' + bool + ');' + '\n';
    return code;
}

///////////////////////////TM1637数码管是否显示冒号////////////////
export const ks_tm1637_mh = function (_, generator) {
    var bool = generator.valueToCode(this, 'BOOL', generator.ORDER_ATOMIC);
    var num1 = generator.valueToCode(this, 'num1', generator.ORDER_ATOMIC);
    var num2 = generator.valueToCode(this, 'num2', generator.ORDER_ATOMIC);


    var code = 'TM.DNum(' + num1 + ',' + num2 + ',' + bool + ');' + '\n';
    return code;
}


////////////////////////1602LCD///////////////////////////
export const ks_1602lcd = function (_, generator) {
    var str1 = generator.valueToCode(this, 'TEXT1', generator.ORDER_ATOMIC) || 'String("")'
    var str2 = generator.valueToCode(this, 'TEXT2', generator.ORDER_ATOMIC) || 'String("")'
    //var bool = generator.valueToCode(this, 'BOOL', generator.ORDER_ATOMIC);
    generator.definitions_['define_i2c'] = '#include <Wire.h>';
    generator.definitions_['define_lcd'] = '#include <LiquidCrystal_I2C.h>';
    generator.definitions_['var_lcd'] = 'LiquidCrystal_I2C lcd(0x27,16,2);';
    generator.setups_['setup_lcd1'] = 'lcd.init();';
    generator.setups_['setup_lcd2'] = 'lcd.backlight();';
    var code = 'lcd.setCursor(0, 0);\n'
    code += 'lcd.print(' + str1 + ');\n';
    code += 'lcd.setCursor(0, 1);\n';
    code += 'lcd.print(' + str2 + ');\n';

    //code+='if('+bool+')\n{\nlcd.clear();\n}';
    return code;
}


////////////////////////1602LCD///////////////////////////
export const ks_1602lcd_clear = function () {
    var code = 'lcd.clear();\n'
    return code;
}


////////////////////////2004LCD///////////////////////////
export const ks_2004lcd = function (_, generator) {
    var str1 = generator.valueToCode(this, 'TEXT1', generator.ORDER_ATOMIC) || 'String("")'
    var str2 = generator.valueToCode(this, 'TEXT2', generator.ORDER_ATOMIC) || 'String("")'
    var str3 = generator.valueToCode(this, 'TEXT3', generator.ORDER_ATOMIC) || 'String("")'
    var str4 = generator.valueToCode(this, 'TEXT4', generator.ORDER_ATOMIC) || 'String("")'

    generator.definitions_['define_i2c'] = '#include <Wire.h>';
    generator.definitions_['define_lcd'] = '#include <LiquidCrystal_I2C.h>';
    generator.definitions_['var_lcd'] = 'LiquidCrystal_I2C lcd(0x27,16,4);';
    generator.setups_['setup_lcd1'] = 'lcd.init();';
    generator.setups_['setup_lcd2'] = 'lcd.backlight();';
    var code = 'lcd.setCursor(0, 0);\n'
    code += 'lcd.print(' + str1 + ');\n';
    code += 'lcd.setCursor(0, 1);\n';
    code += 'lcd.print(' + str2 + ');\n';
    code += 'lcd.setCursor(0, 2);\n';
    code += 'lcd.print(' + str3 + ');\n';
    code += 'lcd.setCursor(0, 3);\n';
    code += 'lcd.print(' + str4 + ');\n';

    return code;
}

////////////////////////1602LCD///////////////////////////
export const ks_2004lcd_clear = function () {
    var code = 'lcd.clear();\n'
    return code;
}


//////////////////////////OLED//////////////////////////////////////
export const ks_oled = function (_, generator) {

    var s = generator.valueToCode(this, 'size', generator.ORDER_ATOMIC);

    var str1 = generator.valueToCode(this, 'TEXT1', generator.ORDER_ATOMIC);
    var str2 = generator.valueToCode(this, 'TEXT2', generator.ORDER_ATOMIC);
    var str3 = generator.valueToCode(this, 'TEXT3', generator.ORDER_ATOMIC);
    var str4 = generator.valueToCode(this, 'TEXT4', generator.ORDER_ATOMIC);

    generator.definitions_['define_Wire'] = '#include <Wire.h>';
    generator.definitions_['define_GFX'] = '#include <Adafruit_GFX.h>';
    generator.definitions_['define_SSD1306'] = '#include <Adafruit_SSD1306.h>';

    generator.definitions_['OLED_RESET'] = '#define OLED_RESET 4';
    generator.definitions_['display'] = 'Adafruit_SSD1306 display(OLED_RESET);';

    generator.setups_['setup_oled1'] = 'display.begin(SSD1306_SWITCHCAPVCC, 0x3C);';
    generator.setups_['setup_oled2'] = 'display.display();';
    generator.setups_['setup_oled3'] = 'delay(2000);';
    generator.setups_['setup_oled4'] = 'display.clearDisplay();';


    var code = 'display.setTextSize(' + s + ');\ndisplay.setTextColor(WHITE);\ndisplay.setCursor(0,0);\ndisplay.println(' + str1 + ');\ndisplay.println(' + str2 + ');\ndisplay.println(' + str3 + ');\ndisplay.println(' + str4 + ');\ndisplay.display();\ndelay(250);\ndisplay.clearDisplay();\n';
    return code;
}

////////////////////////oled_clear///////////////////////////
export const ks_oled_clear = function () {
    var code = 'display.clearDisplay();\n'
    return code;
}

//////////////////////////////点阵初始化/////////////////////////////
export const ks_matrix_init = function (_, generator) {
    var SDA = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var SCL = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);
    var addr = generator.valueToCode(this, 'address', generator.ORDER_ATOMIC);
    var matrixName = this.getFieldValue('matrixName');
    generator.definitions_['include_Matrix'] = '#include <Matrix.h>';
    generator.definitions_[matrixName] = 'Matrix ' + matrixName + '(' + SDA + ',' + SCL + ');';
    generator.setups_['setup_' + matrixName] = matrixName + '.begin(' + addr + '); \n';
    var code = matrixName + '.clear();\n';
    return code;
}


////////////////////////////////点阵1////////////////////////////
//执行器_点阵屏显示_显示图案
export const ks_matrix1 = function (_, generator) {
    var matrixName = this.getFieldValue('matrixName');
    var dotMatrixArray = generator.valueToCode(this, 'LEDArray', generator.ORDER_ASSIGNMENT);
    generator.definitions_['LEDArray'] = 'uint8_t  LEDArray[8];';
    //  var code='Matrix_'+SDA+'_'+SCL+'.clear()dotMatrix;\n';
    var code = '';
    code += 'for(int i=0; i<8; i++)\n';
    code += '{\n'
    code += '  LEDArray[i]=' + dotMatrixArray + '[i];\n';
    code += '  for(int j=7; j>=0; j--)\n'
    code += '  {\n'
    code += '    if((LEDArray[i]&0x01)>0)\n';
    code += '    ' + matrixName + '.drawPixel(j, i,1);\n';
    code += '    LEDArray[i] = LEDArray[i]>>1;\n';
    code += '  }  \n'
    code += '}\n'
    code += matrixName + '.write();\n'
    return code;
}

//执行器_点阵屏显示_点阵数组
export const ks_matrix2 = function (_, generator) {
    var varName = this.getFieldValue('VAR');
    var a = new Array();
    for (let i = 1; i < 9; i++) {
        a[i] = new Array();
        for (let j = 1; j < 9; j++) {
            a[i][j] = (this.getFieldValue('a' + i + j) == "TRUE") ? 1 : 0;
        }
    }
    var code = '{';
    for (let i = 1; i < 9; i++) {
        var tmp = ""
        for (let j = 1; j < 9; j++) {
            tmp += a[i][j];
        }
        tmp = (parseInt(tmp, 2)).toString(16)
        if (tmp.length == 1) tmp = "0" + tmp;
        code += '0x' + tmp + ((i != 8) ? ',' : '');
    }
    code += '};';
    //generator.definitions_[this.id] = "byte LedArray_"+clearString(this.id)+"[]="+code;
    generator.definitions_[varName] = "uint8_t " + varName + "[8]=" + code;
    //return ["LedArray_"+clearString(this.id), generator.ORDER_ATOMIC];
    return [varName, generator.ORDER_ATOMIC];
}

////////////////////////清屏/////////////////////////////
export const ks_Matrix_CLEAR = function () {
    var matrixName = this.getFieldValue('matrixName');
    var code = matrixName + '.clear();\n'
    code += matrixName + '.write();\n';
    return code;
}
////////////////////////////////通讯///////////////////////////////

///////////////////////红外接收///////////////////
export const ks_ir_r = function (_, generator) {
    var variable = generator.variableDB_.getName(this.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
    generator.definitions_['var_declare' + variable] = 'long ' + variable + ';';
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var branch = generator.statementToCode(this, 'DO');
    var branch2 = generator.statementToCode(this, 'DO2');
    generator.definitions_['include_IRremote'] = '#include <IRremote.h>\n';
    //generator.definitions_['var_declare'+varName] = 'long '+varName+';\n';
    generator.definitions_['var_ir_recv' + dropdown_pin] = 'IRrecv irrecv_' + dropdown_pin + '(' + dropdown_pin + ');\ndecode_results results_' + dropdown_pin + ';\n';
    generator.setups_['setup_ir_recv_' + dropdown_pin] = 'irrecv_' + dropdown_pin + '.enableIRIn();';
    var code = "if (irrecv_" + dropdown_pin + ".decode(&results_" + dropdown_pin + ")) {\n"
    code += '  ' + variable + '=results_' + dropdown_pin + '.value;\n';
    code += '  String type="UNKNOWN";\n';
    ////////////////////////////////////////////////////////////////
    code += '  String typelist[14]={"UNKNOWN", "NEC", "SONY", "RC5", "RC6", "DISH", "SHARP", "PANASONIC", "JVC", "SANYO", "MITSUBISHI", "SAMSUNG", "LG", "WHYNTER"};\n';
    code += '  if(results_' + dropdown_pin + '.decode_type>=1&&results_' + dropdown_pin + '.decode_type<=13){\n';
    code += '    type=typelist[results_' + dropdown_pin + '.decode_type];\n'
    code += '  }\n';
    code += '  Serial.print("IR TYPE:"+type+"  ");\n';
    code += branch;
    code += '  irrecv_' + dropdown_pin + '.resume();\n'
    code += '} else {\n';
    code += branch2;
    code += '}\n';
    return code;
}

//////////////////////////////////////红外发射/////////////////////////////
export const ks_ir_s = function (_, generator) {

    var num1 = generator.valueToCode(this, 'num1', generator.ORDER_ATOMIC);

    generator.definitions_['define_IRre.h'] = '#include <IRremote.h>';
    generator.definitions_['define_IRsend'] = 'IRsend irsend;';

    var code = 'irsend.sendRC5(' + num1 + ', 8);\ndelay(200);\n';
    return code;
}

/////////////////////////////////////蓝牙////////////////////////////////////
export const ks_bluetooth = function (_, generator) {
    var val = this.getFieldValue('VAL');
    var branch = generator.statementToCode(this, 'DO');
    var dropdown_pin1 = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var dropdown_pin2 = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);


    generator.definitions_['include_Soft'] = '#include <SoftwareSerial.h>\n';
    generator.definitions_['mySerial'] = 'SoftwareSerial mySerial(' + dropdown_pin1 + ', ' + dropdown_pin2 + ');\n';
    generator.definitions_['char'] = 'char ' + val + ';\n';

    generator.setups_['mySerial23'] = 'mySerial.begin(9600);';

    var code = 'if (mySerial.available())\n{\n' + val + ' = mySerial.read();\n';
    code += branch;
    code += '}\n';
    return code;
}

////////////////////////esp8266///////////////////////////
export const ks_esp8266 = function (_, generator) {
    var str1 = generator.valueToCode(this, 'TEXT1', generator.ORDER_ATOMIC) || 'String("")'
    var str2 = generator.valueToCode(this, 'TEXT2', generator.ORDER_ATOMIC) || 'String("")'
    //var dropdown_stat = this.getFieldValue('STAT');
    //var bool = generator.valueToCode(this, 'BOOL', generator.ORDER_ATOMIC);
    generator.definitions_['WiFiEsp'] = '#include <WiFiEsp.h>';
    generator.definitions_['HAVE'] = '#ifndef HAVE_HWSERIAL1';
    generator.definitions_['SoftwareSerial'] = '#include <SoftwareSerial.h>';
    generator.definitions_['Serial1'] = 'SoftwareSerial Serial1(4,3);';
    generator.definitions_['#endif'] = '#endif';
    generator.definitions_['ssid'] = 'char ssid[] = ' + str1 + ';';
    generator.definitions_['pass'] = 'char pass[] = ' + str2 + ';';
    generator.definitions_['status'] = 'int status = WL_IDLE_STATUS;';

    generator.definitions_['printWifiData'] = 'void printWifiData()\n{\nIPAddress ip = WiFi.localIP();\n  Serial.print("IP Address: ");\n  Serial.println(ip);\n  byte mac[6];\n  WiFi.macAddress(mac);\n  char buf[20];\n  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);\n  Serial.print("MAC address: ");\n  Serial.println(buf);\n}\n';

    generator.definitions_['printCurrentNet'] = 'void printCurrentNet()\n{\nSerial.print("SSID: ");\n  Serial.println(WiFi.SSID());\n  byte bssid[6];\n  WiFi.BSSID(bssid);\n  char buf[20];\n  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);\n  Serial.print("BSSID: ");\n  Serial.println(buf);\n  long rssi = WiFi.RSSI();\n  Serial.print("Signal strength (RSSI): ");\n  Serial.println(rssi);\n}\n';

    generator.setups_['setup_lcd1'] = 'Serial1.begin(115200);\n    WiFi.init(&Serial1);\n    if (WiFi.status() == WL_NO_SHIELD) \n{\n    Serial.println("WiFi shield not present");\n  while (true);\n}\nwhile ( status != WL_CONNECTED) {\n    Serial.print("Attempting to connect to WPA SSID: ");\n    Serial.println(ssid);\n    status = WiFi.begin(ssid, pass);\n  }\n  Serial.println("You are connected to the network");\n';
    var code = 'Serial.println();\n  printCurrentNet();\n  printWifiData();\n  delay(10000);\n';

    return code;
}